package com.hatic.process;

import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.util.MapUtil;

import com.hatic.money.PlanAuthBO;

/**
 * 处理大计划在初步设计阶段创建子计划。
 *
 * @author TonyTan
 * @version 1.0, 2011-11-4
 */
public class ProjectInitBO extends BusinessObject {
	private static final long serialVersionUID = 1L;
	
	//选择投资计划变成大计划
	public String insertDa(String[] planIds) {
		if (planIds == null || planIds.length == 0) {
			setMessage("没有选择投资计划！");
			return _returnFaild;
		}
		
		for (int i = 0, n = planIds.length; i < n; i++) {
			String planId = planIds[i];
				
			updateBaoType(planId,"2");
			updateProjBaoType(planId,"2");
		}
		
		return _returnSuccess;
	}
	//取消导入错了的大计划
	public String delDa(String[] planIds) {
		if (planIds == null || planIds.length == 0) {
			setMessage("没有选择投资计划！");
			return _returnFaild;
		}
		
		for (int i = 0, n = planIds.length; i < n; i++) {
			String planId = planIds[i];
			
			if (hasSubPlan(planId)) {
				setMessage("选择的投资计划包含子计划，不能取消！");
				return _returnFaild;
			}
			
			updateBaoType(planId,"0");
			updateProjBaoType(planId,"0");
		}
		
		return _returnSuccess;
	}
	
	//检查是否有子计划
	private boolean hasSubPlan(String planId) {
		String sql = "select count(*) as cnt from money_plan where plan_parentid = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(planId);
		
		return MapUtil.hasRecord(_dao.queryMap(param));
	}
	
	//更新计划包类型
	private boolean updateBaoType(String planId,String baoType) {
		String sql = "update money_plan set bao_type = ? where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addIntValue(baoType);
		param.addStringValue(planId);
		
		return _dao.update(param);
	}
	
	
	//更新投资计划对应的项目的包类型
	private boolean updateProjBaoType(String planId,String baoType) {
		String sql = "update project_base set bao_type = ? ,projbao_type = ? where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addIntValue(baoType);
		param.addIntValue(baoType);
		param.addStringValue(planId);
		
		return _dao.update(param);
	}

	//子计划提交前的处理：把父计划中的相关信息更新到子计划中，同时创建子项目
	public String preAudit(String[] planIds, String userId) {
		if (planIds == null || planIds.length == 0) {
			setMessage("没有选择子投资计划！");
			return _returnFaild;
		}
		
		for (int i = 0, n = planIds.length; i < n; i++) {
			String planId = planIds[i];
			
			createSubProject(planId, userId);
		}
		
		return _returnSuccess;
	}
	
	/**
	 * 创建子项目
	 * @param planId
	 * @param userId
	 * @return
	 */
	public boolean createSubProject(String planId, String userId) {
		PlanAuthBO planbo = new PlanAuthBO();
		
		String parentId = getParentId(planId);
		
		//把大计划中的部分信息更新到子计划中
		if (!updateParent(planId, parentId)) {
			setMessage("把大计划中的部分信息更新到子计划中出错！");
			return false;
		}
		
		//产生子计划的立项申请
		if (!planbo.projectCreate(planId, userId, parentId)) {
			setMessage("投资计划包创建项目立项申请出错！");
			return false;
		}
		
		return true;
	}
	
	//把父计划中的相关信息更新到子计划中
	private boolean updateParent(String planId, String parentId) {
		if (parentId == null || parentId.length() == 0) return true; 
		
		Map<String,String> mpData = queryParent(parentId);
		
		String sql = "update money_plan set auth_type = ?, done_deptid = ?, " +
				"done_deptname = ?, check_fileno = ?, check_name = ?, check_right2 = ? " +
				"where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(mpData.get("auth_type"));
		param.addStringValue(mpData.get("done_deptid"));
		param.addStringValue(mpData.get("done_deptname"));
		param.addStringValue(mpData.get("check_fileno"));
		param.addStringValue(mpData.get("check_name"));
		param.addStringValue(mpData.get("check_right2"));
		param.addStringValue(planId);
		
		return _dao.update(param);
	}
	
	//取父计划的相关授权信息
	private Map<String,String> queryParent(String planId) {
		String sql = "select auth_type, done_deptid, done_deptname, check_fileno, check_name, check_right2 " +
					 "from money_plan where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(planId);
		
		return _dao.queryMap(param);
	}
	
	//取父投资计划ID
	private String getParentId(String planId) {
		String sql = "select plan_parentid from money_plan where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(planId);
		
		Map<String, String> mpData = _dao.queryMap(param);
		return mpData.get("plan_parentid");
	}
}
